#include<iostream>
#include<set>
#include<vector>
using namespace std;

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int ansl = 2000000000;
        long long sum = nums[0];
        int l = 0, r = 0;
        int len = nums.size();
        while (1) {
            if (sum < target) {
                ++r;
                if (r >= len) break;
                sum += nums[r];
            }
            else {
                if (l >= r) return 1;
                ansl = min(ansl, r - l + 1);
                sum -= nums[l];
                ++l;
            }
        }
        if (ansl == 2000000000) return 0;
        return ansl;
    }
};


class Solution1 {
public:
    int lengthOfLongestSubstring(string s) {
        set<char> se;
        // if(s.size() == 1) return 1;
        int ans = 0;
        int len = s.size();
        int rk = 0;
        for (int i = 0; i < len; i++) {
            while (!se.insert(s[i]).second) {
                ans = max(ans, (int)se.size());
                se.erase(s[rk++]);
            }
        }
        ans = max(ans, (int)se.size());
        return ans;
    }
};

class Solution2 {
public:
    int lengthOfLongestSubstring(string s) {
        set<char> se;
        int ans = 0;
        int len = s.size();
        int rk = 0;
        for (int i = 0; i < len; i++) {
            while (!se.insert(s[i]).second) {
                se.erase(s[rk++]);
            }
            ans = max(ans, (int)se.size());
        }
        return ans;
    }
};

class Solution3 {
public:
    int longestOnes(vector<int>& nums, int k) {
        int one = 0, zero = 0;
        int len = nums.size();
        int ans = 0;
        int left = 0;
        for (int i = 0; i < len; ++i) {
            if (nums[i]) ++one;
            else ++zero;
            ans = min((int)nums.size(), max(ans, one + k));
            while (zero > k && left <= i) {
                if (nums[left]) --one;
                else --zero;
                ++left;
            }
        }
        return ans;
    }
};

class Solution4 {
public:
    int minOperations(vector<int>& nums, int x) {
        long long target = 0;
        for (auto& e : nums) target += e;
        target -= x;
        if (target == 0) return nums.size();
        int ans_l = -1, ans_r = -1;
        int left = 0, len = nums.size();
        long long sum = 0; int ans_sub = -1;
        for (int i = 0; i < len; i++) {
            sum += nums[i];
            while (sum > target && left <= i) {
                sum -= nums[left];
                ++left;
            }
            if (sum == target && ans_sub < i - left) {
                ans_sub = i - left;
                ans_l = left;
                ans_r = i;
            }
        }
        // cout << ans_l << " " << ans_r << " " << ans_sub;
        if (ans_l == -1) return -1;
        return len - ans_r - 1 + ans_l;
    }
};

class Solution5 {
public:
    int totalFruit(vector<int>& fruits) {
        set<int> se;
        int ans = 0, len = fruits.size();
        int left = 0, tmp = 0;
        for (int i = 0; i < len; ++i) {
            se.insert(fruits[i]);
            if (se.size() <= 2) ++tmp;
            else {
                ans = max(ans, tmp);
                tmp = 2;
                for (int k = i - 2; fruits[i - 1] == fruits[k] && k >= 0; --k) {
                    ++tmp;
                }
                se.clear();
                se.insert(fruits[i]);
                se.insert(fruits[i - 1]);
            }
        }
        ans = max(ans, tmp);
        return ans;
    }
};

class Solution6 {
public:
    int arrp[26];
    int arrs[26];
    vector<int> findAnagrams(string s, string p) {
        if (s.size() < p.size())return {};
        vector<int> ans;
        ans.reserve(40);
        for (auto& ch : p) ++arrp[ch - 'a'];
        int left = 0, right = p.size();
        for (int i = 0; i < p.size(); i++) ++arrs[s[i] - 'a'];
        for (; right <= s.size(); ++right, ++left) {
            int flag = 1;
            for (int i = 0; i < 26; i++) {
                if (arrp[i] != arrs[i]) {
                    flag = 0;
                    break;
                }
            }
            if (flag) ans.push_back(left);
            if (right != s.size()) {
                --arrs[s[left] - 'a'];
                ++arrs[s[right] - 'a'];
            }
        }
        return ans;
    }
};

